package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

/**
 * @author Cc
 * @version 1.0
 * <p>
 * 专题管理
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/topic")
public class AdminTopicController {

    @Autowired
    private DtsTopicService dtsTopicService;


    /**
     * 专题列表
     * 接口地址 : http://localhost:8083/admin/topic/list?page=1&limit=20&sort=add_time&order=desc
     */

    @RequiresPermissions("admin:topic:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String title, String sort, String order,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit) {


        IPage<DtsTopic> storageIPage = dtsTopicService.page(page, limit, sort, order);

        HashMap map = new HashMap();
        map.put("total", storageIPage.getTotal());
        map.put("items", storageIPage.getRecords());

        return ResponseUtil.ok(map);
    }

    /**
     * 接口地址 : http://localhost:8083/admin/topic/create
     */
    @RequiresPermissions("admin:topic:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody String body) throws ParseException {
        DtsTopic topic = new DtsTopic();
        topic.setTitle(JacksonUtil.parseString(body, "title"))
                .setSubtitle(JacksonUtil.parseString(body, "subtitle"))
                .setPrice(new BigDecimal(JacksonUtil.parseInteger(body, "price")))
                .setReadCount(JacksonUtil.parseString(body, "readCount"))
                .setGoods(JacksonUtil.parseString(body, "goods"))
                .setPicUrl(JacksonUtil.parseString(body, "picUrl"))
                .setAddTime(new Date())
                .setUpdateTime(new Date())
                .setShareUrl(JacksonUtil.parseString(body, "shareUrl"))
                .setContent(JacksonUtil.parseString(body, "content"));

        dtsTopicService.addTopic(topic);
        return ResponseUtil.ok(topic);

    }
    /**
     *      专题修改
     *          接口地址 : http://localhost:8083/admin/topic/update
     */
    @RequiresPermissions("admin:topic:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "修改")
    @PostMapping("/update")
    public  Object update(@RequestBody DtsTopic topic){
        dtsTopicService.updateTopic(topic);

        return ResponseUtil.ok();
    }

    /**
     * 专题删除
     *      接口地址 : http://localhost:8083/admin/topic/delete
     * @param topic
     * @return
     */
    @RequiresPermissions("admin:topic:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsTopic topic){
        Integer id = topic.getId();
        dtsTopicService.deleteTopic(id);
        return  ResponseUtil.ok();
    }
}
